import React from 'react';
import { PopConfirm } from '@teamix/ui';

export default class Confirm extends React.Component {
  state = {
    visible: false,
  };

  onCancel = () => {
    this.setState({
      visible: false,
    });
    this.props.onVisibleChange(false);
  };

  onConfirm = () => {
    this.setState({
      visible: false,
    });
    this.props.confirm();
    this.props.onVisibleChange(false);
  };

  onVisibleChange = (visible) => {
    this.setState({
      visible,
    });
    this.props.onVisibleChange(visible);
  };

  render() {
    const { visible } = this.state;
    const { trigger, type, title, content, icon, align } = this.props;
    return (
      <PopConfirm
        icon={icon}
        type={type}
        triggerType="click"
        align={align || 'r'}
        title={title}
        content={content}
        onConfirm={this.onConfirm}
        onCancel={this.onCancel}
        visible={visible}
        onVisibleChange={this.onVisibleChange}
        trigger={trigger}
      />
    );
  }
}
